package fr.upstlse.glre.client.administration.depot.action;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.InfoConfig;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.espacededie.commun.depot.action.AbstractViewerSelectionAction;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.utilitaires.UtilGWT;

public class AdminViewerSelectionAction extends AbstractViewerSelectionAction {
	Window window;
	
	public AdminViewerSelectionAction(DataList viewer) {
		super(viewer);
	}

	@Override
	protected void action(BaseEvent be) {
		window = new Window();
		window.setWidth(450);
		window.setModal(true);
		window.setClosable(true);
		window.setHeading(RfgConstante.TELECHARGER_WINDOW_HEADER_TEXT);
		window.setBodyStyle("telechargerWindowStyle");
		Info.display("DEBUG", "Avant getselecteditem", "");
		ModelData lDocument = viewer.getSelectedItem().getModel();

		Label lIntituleLabel = new Label((String) lDocument
				.get(RfgConstante.DEPOT_MODELDATA_INTITULE));
		lIntituleLabel.addStyleName("telechargerWindowStyleIntitule");
		window.add(lIntituleLabel);
		Label lDescriptionLabel = new Label((String) lDocument
				.get(RfgConstante.DEPOT_MODELDATA_DESCRIPTION));
		lDescriptionLabel.addStyleName("telechargerWindowStyleDescription");
		window.add(lDescriptionLabel);
		Label lTailleLabel = new Label(UtilGWT.getTaille(lDocument.get(
				RfgConstante.DEPOT_MODELDATA_TAILLE).toString()));
		lTailleLabel.addStyleName("telechargerWindowStyleTaille");
		window.add(lTailleLabel);

		//contain the hidden field with documentId
		final FormPanel lFormPanel = new FormPanel();
		lFormPanel.setAction(GWT.getModuleBaseURL() + "download");
		lFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		lFormPanel.setMethod(FormPanel.METHOD_POST);

		VerticalPanel lVerticalPanel = new VerticalPanel();
		lVerticalPanel.setHorizontalAlign(HorizontalAlignment.RIGHT);
		final Hidden lDocumentId = new Hidden(
				RfgConstante.TELECHARGER_WINDOW_HIDDEN_NAME, (lDocument
						.get(RfgConstante.DEPOT_MODELDATA_ID).toString()));
		lVerticalPanel.add(lDocumentId);

		com.google.gwt.user.client.ui.Button lTelechargerBouton = new com.google.gwt.user.client.ui.Button(
				RfgConstante.TELECHARGER_WINDOW_BUTTON_TEXT,
				new ClickListener() {
					public void onClick(Widget sender) {
						lFormPanel.submit();
					}
				});
		lVerticalPanel.add(lTelechargerBouton);
		
		com.google.gwt.user.client.ui.Button lDeleteBouton = new com.google.gwt.user.client.ui.Button(
				RfgConstante.TELECHARGER_DELETE_WINDOW_BUTTON_TEXT,
				new ClickListener() {
					public void onClick(Widget sender) {
						final MessageBox lAttente = MessageBox.alert("Information",
								"Suppression du document en cours...", null);
						AsyncCallback<Object> lAsyncCallback = new AsyncCallback<Object>() {

							public void onFailure(Throwable pCaught) {
								lAttente.hide();
								MessageBox.alert("Erreur", "Erreur deleteDocument "
										+ pCaught.getMessage(), null);
								
							}

							public void onSuccess(Object pResult) {
								lAttente.hide();
								window.hide();
								Info.display("Information", "Document supprimé", "");
							}
						};
						ControlerUtil.getControlerServiceAsyncInstance().deleteDocument(new Long(lDocumentId.getValue()), lAsyncCallback);
					}
				});
		lVerticalPanel.add(lDeleteBouton);

		lFormPanel.addFormHandler(new FormHandler() {

			public void onSubmit(FormSubmitEvent arg0) {
			}

			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				InfoConfig info = new InfoConfig("Information", "Téléchargement de "
						+ event.getResults() + " en cours");
				info.display = 5000;
				Info.display(info);
			}
		});

		lFormPanel.add(lVerticalPanel);
		window.add(lFormPanel);
		window.show();
	}

}
